home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter5 / remove.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  237 lines

  1.  
  2. #include <windows.h>  
  3. #include "remove.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "ImageList_Remove()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HIMAGELIST hList  = NULL;
  110. static int        nSize  = 32;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE :
  115.               InitCommonControls();
  116.  
  117.               // Create the image list.
  118.               //.......................
  119.               hList = ImageList_Create( nSize, nSize, ILC_COLOR | ILC_MASK, 1, 1 );
  120.               break;
  121.  
  122.       case WM_PAINT :
  123.               {
  124.                  PAINTSTRUCT ps;
  125.                  int         i, nItems;
  126.  
  127.                  // Retrieve the number of images.
  128.                  //...............................
  129.                  nItems = ImageList_GetImageCount( hList );
  130.  
  131.                  // Paint the images on the client area.
  132.                  //.....................................
  133.                  BeginPaint( hWnd, &ps );
  134.                  for ( i=0; i<nItems; i++ )
  135.                     ImageList_Draw( hList, i, ps.hdc, (i*nSize)+10, 10,
  136.                                  (i == nItems - 1) ? 
  137.                                  ILD_SELECTED : ILD_NORMAL);
  138.                  EndPaint( hWnd, &ps );
  139.               }
  140.               break;
  141.  
  142.       case WM_COMMAND :
  143.               switch( LOWORD( wParam ) )
  144.               {
  145.                  case IDM_ARROW :
  146.                  case IDM_WAITING :
  147.                  case IDM_WORKING :
  148.                         ImageList_AddIcon( hList, LoadCursor( NULL, 
  149.                                                     LOWORD( wParam ) == IDM_ARROW ? IDC_ARROW :
  150.                                                     LOWORD( wParam ) == IDM_WAITING ? IDC_WAIT :
  151.                                                     IDC_APPSTARTING ) );
  152.                         InvalidateRect( hWnd, NULL, TRUE );
  153.                         break;
  154.  
  155.                  case IDM_REPARROW :
  156.                  case IDM_REPWAITING :
  157.                  case IDM_REPWORKING :
  158.                         ImageList_ReplaceIcon( hList, ImageList_GetImageCount( hList )-1,
  159.                                                LoadCursor( NULL, 
  160.                                                 LOWORD( wParam ) == IDM_REPARROW ? IDC_ARROW :
  161.                                                 LOWORD( wParam ) == IDM_REPWAITING ? IDC_WAIT : 
  162.                                                 IDC_APPSTARTING ) );
  163.                         InvalidateRect( hWnd, NULL, TRUE );
  164.                         break;
  165.  
  166.                  case IDM_REMITEM :
  167.                         ImageList_Remove( hList, ImageList_GetImageCount( hList ) - 1 );
  168.                         InvalidateRect( hWnd, NULL, TRUE );
  169.                         break;
  170.  
  171.                  case IDM_REMALL :
  172.                         ImageList_RemoveAll( hList );
  173.                         InvalidateRect( hWnd, NULL, TRUE );
  174.                         break;
  175.  
  176.                  case IDM_ENLARGE :
  177.                         nSize += 5;
  178.                         ImageList_SetIconSize( hList, nSize, nSize );
  179.                         InvalidateRect( hWnd, NULL, TRUE );
  180.                         break;
  181.  
  182.                  case IDM_REDUCE :
  183.                         nSize -= 5;
  184.                         ImageList_SetIconSize( hList, nSize, nSize );
  185.                         InvalidateRect( hWnd, NULL, TRUE );
  186.                         break;
  187.  
  188.                  case IDM_ABOUT :
  189.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  190.                         break;
  191.  
  192.                  case IDM_EXIT :
  193.                         DestroyWindow( hWnd );
  194.                         break;
  195.               }
  196.               break;
  197.       
  198.       case WM_DESTROY :
  199.               if ( hList )
  200.                  ImageList_Destroy( hList );
  201.  
  202.               PostQuitMessage(0);
  203.               break;
  204.  
  205.       default :
  206.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  207.    }
  208.  
  209.    return( 0L );
  210. }
  211.  
  212.  
  213.  
  214.  
  215. LRESULT CALLBACK About( HWND hDlg,           
  216.                         UINT message,        
  217.                         WPARAM wParam,       
  218.                         LPARAM lParam)
  219. {
  220.    switch (message) 
  221.    {
  222.        case WM_INITDIALOG: 
  223.                return (TRUE);
  224.  
  225.        case WM_COMMAND:                              
  226.                if (   LOWORD(wParam) == IDOK         
  227.                    || LOWORD(wParam) == IDCANCEL)    
  228.                {
  229.                        EndDialog(hDlg, TRUE);        
  230.                        return (TRUE);
  231.                }
  232.                break;
  233.    }
  234.  
  235.    return (FALSE); 
  236. }
  237.